package rm.ep.server;

import java.util.List;

import rm.ep.shared.EntityExistsException;
import rm.ep.shared.KeywordEntity;

import com.googlecode.objectify.util.DAOBase;

public class KeywordDAO extends DAOBase {

	public KeywordEntity insert(String keyword) 
			throws EntityExistsException {
		KeywordEntity kw = ofy().query(KeywordEntity.class).filter("fKeyword", keyword).get();
		if (kw != null) throw new EntityExistsException(keyword.toLowerCase());
		kw = new KeywordEntity(keyword);
		ofy().put(kw);
		return kw;
	}
	
	public KeywordEntity reActivate(Long id) {
		KeywordEntity e = ofy().get(KeywordEntity.class, id);
		e.setActive(true);
		ofy().put(e);
		return e;
	}
	
	public KeywordEntity deActivate(Long id) {
		KeywordEntity e = ofy().get(KeywordEntity.class, id);
		e.setActive(false);
		ofy().put(e);
		return e;
	}
	
	public List<KeywordEntity> selectActive() {
		return ofy().query(KeywordEntity.class).filter("fIsActive", true).list();
	}

	public List<KeywordEntity> selectInActive() {
		return ofy().query(KeywordEntity.class).filter("fIsActive", false).list();
	}

}
